package com.yamanyar.weebill.domain;

//Generated Jan 31, 2008 6:26:41 PM by Hibernate Tools 3.2.0.CR1

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.validator.NotNull;

/**
 * <code>Project</code> represents a project that the <code>Component</code>s
 * will be grouped under. <code>Component</code>s will group bugs or tasks.
 * Also bug or a task can directly linked to a <code>Project</code>.
 * 
 * @author Kaan Yamanyar
 */
@Entity
public class Project implements java.io.Serializable {

	private static final long serialVersionUID = -1726084152730356886L;

	private Long id;

	private User owner;

	private String name;

	private String description;

	private String projectUrl;

	private Set<Attachment> attachments = new HashSet<Attachment>();

	private Set<Component> components = new HashSet<Component>();

	/**
	 * Returns the component that this project contains.
	 * 
	 * A project can contain many components but a component can has only one
	 * project.
	 * 
	 * @return The components that this project contains.
	 * @see Component
	 */
	@OneToMany
	@JoinColumn(name = "projectId")
	// projectId will be created in component object's table
	// TODO FIX THIS Collection(child = true, inverse = "project")
	public Set<Component> getComponents() {
		return this.components;
	}

	/**
	 * Sets the component that this project contains.
	 * 
	 * A project can contain many components but a component can has only one
	 * project.
	 * 
	 * @param components
	 *            Component of the project
	 * @see Component
	 */
	public void setComponents(Set<Component> components) {
		this.components = components;
	}

	/**
	 * Returns the description of the project.
	 * 
	 * @return Description of the project. Max 400 chars allowed.
	 */
	@Column(length = 400)
	@NotNull
	public String getDescription() {
		return this.description;
	}

	/**
	 * 
	 * @param description
	 *            Description of the project. Max 400 chars allowed.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 
	 * @return Name of the project. Max 60 chars allowed.
	 */

	@NotNull
	@Column(length = 60)
	public String getName() {
		return this.name;
	}

	/**
	 * 
	 * @param name
	 *            Name of the project. Max 60 chars allowed.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @return URL of the project if it exists. Max 250 chars allowed.
	 */
	@Column(length = 250)
	public String getProjectUrl() {
		return this.projectUrl;
	}

	/**
	 * 
	 * @param projectUrl
	 *            URL of the project.Max 250 chars allowed.
	 */
	public void setProjectUrl(String projectUrl) {
		this.projectUrl = projectUrl;
	}

	/**
	 * Return the owner of the project.
	 * 
	 * Projects are owned by an user. Owner is responsible for adding other
	 * users with some roles (like admin,developer, tester, etc.).
	 * 
	 * A project can only have one owner. But an user can have many projects.
	 * 
	 * @return Owner of the project.
	 * @see User
	 */
	@ManyToOne
	@JoinColumn(name = "owner_id")
	// owner id will be created in Project Object's table
	@NotNull
	public User getOwner() {
		return this.owner;
	}

	/**
	 * Sets the owner of the project.
	 * 
	 * Projects are owned by an user. Owner is responsible for adding other
	 * users with some roles (like admin, developer, tester, etc.).
	 * 
	 * A project can only have one owner. But an user can have many projects.
	 * 
	 * 
	 * @param owner
	 */
	public void setOwner(User owner) {
		this.owner = owner;
	}

	/**
	 * 
	 * @return The id of user. Id is auto-generated by db.
	 */
	@Id
	@GeneratedValue
	public Long getId() {
		return this.id;
	}

	/**
	 * 
	 * @return Attachments attached to this project.
	 * @see Attachment
	 */
	@ManyToMany(targetEntity = Attachment.class, cascade = CascadeType.ALL)
	// TODO FIX Collection(child = true)
	public Set<Attachment> getAttachments() {
		return this.attachments;
	}

	/**
	 * 
	 * @param attachments
	 *            Set of <code>Attachment</code>s
	 * @see Attachment
	 */
	public void setAttachments(Set<Attachment> attachments) {
		this.attachments = attachments;
	}

	/**
	 * Overridden to shot name of the project.
	 */
	@Override
	public String toString() {
		return this.name;
	}

	@Override
	public int hashCode() {
		if (this.id == null) {
			return 0;
		}
		return (int) (this.id / 10) + 1;
	}

	/**
	 * This method is not supposed to be called by a developer.
	 * 
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

}
